package com.example.demo.controller;


import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.config.AppConfig;
import com.example.demo.model.Article;
import com.example.demo.model.ArticleReply;
import com.example.demo.model.User;
import com.example.demo.services.IArticleReplyService;
import com.example.demo.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/reply")
@Slf4j
@Api(tags = "回复接⼝")
public class ArticleReplyController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArticleReplyService articleReplyService;


    /**
     * 回复帖⼦
     *
     * @param articleId 帖⼦Id
     * @param content 回复内容
     * @return AppResult
     */
    @ApiOperation("回复帖⼦")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖⼦Id") @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content) {
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);

        //用户被禁言
        if(user.getState()==1)
        {
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        Article article=articleService.selectById(articleId);
        //帖子不存在
        if(article==null || article.getDeleteState()==1)
        {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //帖子被封
        if(article.getState()==1)
        {
            return  AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        ArticleReply articleReply=new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReplyService.create(articleReply);

            return AppResult.success();
    }

    /**
     * 回复帖⼦
     *
     * @param articleId 帖⼦Id
     * @return AppResult
     */
    @ApiOperation("回复帖⼦")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam(value = "帖⼦Id")
                                @RequestParam(value = "articleId") @NonNull Long articleId)
    {
        Article article=articleService.selectById(articleId);
        if(article==null || article.getDeleteState()==1)
        {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        List<ArticleReply> articleReplies=articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
